/*
 * This file is part of DALS - Distributed Artificial Life Simulation.
 * DALS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Foobar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DALS.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * EnvironmentConnections.java
 *
 * Created on 25/10/2010, 16:33:33
 */

package genetic.view;

import genetic.controller.EnvironmentConnectionsController;
import genetic.net.EnvironmentProtocol;
import genetic.util.Configuration;
import java.awt.Color;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

/**
 *
 * @author romulo
 */
public class EnvironmentConnections extends javax.swing.JFrame {
    private boolean north = false,
            south = false,
            west = false,
            east = false;
    private Color ACTIVE_COLOR = new Color(140,200,150),
            INACTIVE_COLOR = new Color(255,200,190);
    private EnvironmentConnectionsController controller = new EnvironmentConnectionsController();

    /** Creates new form EnvironmentConnections */
    public EnvironmentConnections() {
        initComponents();
        postInitComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonNorth = new javax.swing.JButton();
        jButtonSouth = new javax.swing.JButton();
        jButtonWest = new javax.swing.JButton();
        jButtonEast = new javax.swing.JButton();
        jPanelMap = new javax.swing.JPanel();
        jLabelDisconnected = new javax.swing.JLabel();
        jLabelConnected = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButtonNorth.setText("North");
        jButtonNorth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNorthActionPerformed(evt);
            }
        });

        jButtonSouth.setText("South");
        jButtonSouth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSouthActionPerformed(evt);
            }
        });

        jButtonWest.setText("West");
        jButtonWest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonWestActionPerformed(evt);
            }
        });

        jButtonEast.setText("East");
        jButtonEast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEastActionPerformed(evt);
            }
        });

        jPanelMap.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanelMapLayout = new javax.swing.GroupLayout(jPanelMap);
        jPanelMap.setLayout(jPanelMapLayout);
        jPanelMapLayout.setHorizontalGroup(
            jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 166, Short.MAX_VALUE)
        );
        jPanelMapLayout.setVerticalGroup(
            jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 110, Short.MAX_VALUE)
        );

        jLabelDisconnected.setText("Not linked");

        jLabelConnected.setText("Linked");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jButtonWest)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelMap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonEast)
                .addGap(75, 75, 75))
            .addGroup(layout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButtonSouth)
                    .addComponent(jButtonNorth))
                .addContainerGap(177, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelDisconnected)
                .addContainerGap(320, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelConnected)
                .addContainerGap(346, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jButtonNorth)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonWest)
                            .addComponent(jButtonEast)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanelMap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonSouth)
                .addGap(5, 5, 5)
                .addComponent(jLabelConnected)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelDisconnected)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonNorthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNorthActionPerformed
        getInputAndProcess(EnvironmentProtocol.DIRECTION_NORTH);
    }//GEN-LAST:event_jButtonNorthActionPerformed

    private void jButtonSouthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSouthActionPerformed
        getInputAndProcess(EnvironmentProtocol.DIRECTION_SOUTH);
    }//GEN-LAST:event_jButtonSouthActionPerformed

    private void jButtonWestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonWestActionPerformed
        getInputAndProcess(EnvironmentProtocol.DIRECTION_WEST);
    }//GEN-LAST:event_jButtonWestActionPerformed

    private void jButtonEastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEastActionPerformed
        getInputAndProcess(EnvironmentProtocol.DIRECTION_EAST);
    }//GEN-LAST:event_jButtonEastActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EnvironmentConnections().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEast;
    private javax.swing.JButton jButtonNorth;
    private javax.swing.JButton jButtonSouth;
    private javax.swing.JButton jButtonWest;
    private javax.swing.JLabel jLabelConnected;
    private javax.swing.JLabel jLabelDisconnected;
    private javax.swing.JPanel jPanelMap;
    // End of variables declaration//GEN-END:variables

    private void postInitComponents() {
        updateDirectionsStatus();

        jLabelConnected.setForeground(ACTIVE_COLOR);
        jLabelDisconnected.setForeground(INACTIVE_COLOR);
    }

    /**
     * Paints the background of given JButton, accordingly with the active flag
     * @param button
     * @param active
     */
    private void paintButton(javax.swing.JButton button, boolean active) {
        if( active ) {
            button.setBackground(ACTIVE_COLOR);
        } else {
            button.setBackground(INACTIVE_COLOR);
        }
    }

    /**
     * requires user input throught a JOptionPane
     * @return
     */
    private String inputConnectionUser() {
        return JOptionPane.showInputDialog(this, "Insira o identificador do ecossistema", "", JOptionPane.QUESTION_MESSAGE);
    }

    /**
     * get the user input, process it and send action to controller
     * @param direction
     */
    private void getInputAndProcess(String direction) {
        String user = inputConnectionUser();
        controller.setDirectionUser(direction, user);
        updateDirectionsStatus();
    }

    /**
     * Retrieves connections status from controller and paints the direction
     * buttons accordingly
     */
    private void updateDirectionsStatus() {
        north = controller.getNorthStatus();
        south = controller.getSouthStatus();
        east = controller.getEastStatus();
        west = controller.getWestStatus();

        paintButton(jButtonNorth, north);
        paintButton(jButtonSouth, south);
        paintButton(jButtonWest, west);
        paintButton(jButtonEast, east);
    }

}
